<?php

/**
 * 跑腿设置
 */

namespace app\admin\controller;

use think\Lang;
use think\Validate;
/**
 * ============================================================================
 * DSMall多用户商城
 * ============================================================================
 * 版权所有 2014-2028 长沙德尚网络科技有限公司，并保留所有权利。
 * 网站地址: http://www.csdeshang.com
 * ----------------------------------------------------------------------------
 * 这不是一个自由软件！您只能在不用于商业目的的前提下对程序代码进行修改和使用 .
 * 不允许对程序代码以任何形式任何目的的再发布。
 * ============================================================================
 * 控制器
 */
class  O2oErrand extends AdminControl {

    public function _initialize() {
        parent::_initialize(); // TODO: Change the autogenerated stub
        Lang::load(APP_PATH . 'admin/lang/' . config('default_lang') . '/o2o_errand.lang.php');
    }
    public function other() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $this->assign('list_config', $list_config);
            $this->setAdminCurItem('other');
            return $this->fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_deposit'] = abs(floatval(input('param.o2o_errand_deposit')));
            if($update_array['o2o_errand_deposit']==0){
                $this->error('保证金必须大于0');
            }
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_other'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/other');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_other'), 0);
            }
        }
    }
    public function distance_price() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $list_config['o2o_errand_distance_price']= unserialize($list_config['o2o_errand_distance_price']);
            $this->assign('list_config', $list_config['o2o_errand_distance_price']);
            $this->setAdminCurItem('distance_price');
            return $this->fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_distance_price'] = $this->formatO2oErrandDistancePrice();
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_distance_price'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/distance_price');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_distance_price'), 0);
            }
        }
    }
    
    public function weight_price() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $list_config['o2o_errand_weight_price']= unserialize($list_config['o2o_errand_weight_price']);
            $this->assign('list_config', $list_config['o2o_errand_weight_price']);
            $this->setAdminCurItem('weight_price');
            return $this->fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_weight_price'] = $this->formatO2oErrandWeightPrice();
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_weight_price'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/weight_price');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_weight_price'), 0);
            }
        }
    }
    
    public function time_price() {
        $config_model = model('config');
        if (!request()->isPost()) {
            $list_config = rkcache('config', true);
            $list_config['o2o_errand_time_price']= unserialize($list_config['o2o_errand_time_price']);
            $this->assign('list_config', $list_config['o2o_errand_time_price']);
            $this->setAdminCurItem('time_price');
            return $this->fetch();
        } else {
            $update_array=array();
            $update_array['o2o_errand_time_price'] = $this->formatO2oErrandTimePrice();
            $result = $config_model->editConfig($update_array);
            if ($result) {
                dkcache('config');
                $this->log(lang('ds_edit') . lang('o2o_errand_time_price'), 1);
                $this->success(lang('ds_common_save_succ'), 'O2oErrand/time_price');
            } else {
                $this->log(lang('ds_edit') . lang('o2o_errand_time_price'), 0);
            }
        }
    }
    
    
    
    private function formatO2oErrandDistancePrice() {
        $o2o_errand_distance_price = input('param.o2o_errand_distance_price/a');
        if (!$o2o_errand_distance_price) {
            $this->error(lang('o2o_errand_distance_price_require'));
        }
        $o2o_errand_distance_price = array_values($o2o_errand_distance_price);
        $len = count($o2o_errand_distance_price);
        //冒泡排序法，按照从小到大排序
        for ($i = 1; $i < $len; $i++) {
            for ($j = 0; $j < $len - $i; $j++) {
                $o2o_errand_distance_price[$j]['start_distance']=($j>0 && isset($o2o_errand_distance_price[$j-1]['end_distance']))?$o2o_errand_distance_price[$j-1]['end_distance']:0;
                $o2o_errand_distance_price[$j]['end_distance']=abs(floatval($o2o_errand_distance_price[$j]['end_distance']));
                $o2o_errand_distance_price[$j]['interval_distance']=abs(floatval($o2o_errand_distance_price[$j]['interval_distance']));
                $o2o_errand_distance_price[$j]['price']=abs(floatval($o2o_errand_distance_price[$j]['price']));
                if(!$o2o_errand_distance_price[$j]['if_fixed'] && $o2o_errand_distance_price[$j]['interval_distance']==0){
                    $this->error('非固定费用时距离增量不能为0');
                }
                $o2o_errand_distance_price[$j+1]['start_distance']=$o2o_errand_distance_price[$j]['end_distance'];
                $o2o_errand_distance_price[$j+1]['end_distance']=abs(floatval($o2o_errand_distance_price[$j+1]['end_distance']));
                $o2o_errand_distance_price[$j+1]['interval_distance']=abs(floatval($o2o_errand_distance_price[$j+1]['interval_distance']));
                $o2o_errand_distance_price[$j+1]['price']=abs(floatval($o2o_errand_distance_price[$j+1]['price']));
                if(!$o2o_errand_distance_price[$j+1]['if_fixed'] && $o2o_errand_distance_price[$j+1]['interval_distance']==0){
                    $this->error('非固定费用时距离增量不能为0');
                }
                if ($o2o_errand_distance_price[$j]['end_distance'] > $o2o_errand_distance_price[$j + 1]['end_distance']) {
                    $temp = $o2o_errand_distance_price[$j + 1];
                    $o2o_errand_distance_price[$j + 1] = $o2o_errand_distance_price[$j];
                    $o2o_errand_distance_price[$j] = $temp;
                }
                $o2o_errand_distance_price[$j]['title']=$o2o_errand_distance_price[$j]['start_distance'].'~'.$o2o_errand_distance_price[$j]['end_distance'].lang('o2o_errand_kilometre');
                $o2o_errand_distance_price[$j]['content']=$o2o_errand_distance_price[$j]['if_fixed']?($o2o_errand_distance_price[$j]['price'].lang('ds_yuan')):sprintf(lang('o2o_errand_distance_content'),$o2o_errand_distance_price[$j]['interval_distance'],$o2o_errand_distance_price[$j]['price']);
                $o2o_errand_distance_price[$j+1]['title']=$o2o_errand_distance_price[$j+1]['start_distance'].'~'.$o2o_errand_distance_price[$j+1]['end_distance'].lang('o2o_errand_kilometre');
                $o2o_errand_distance_price[$j+1]['content']=$o2o_errand_distance_price[$j+1]['if_fixed']?($o2o_errand_distance_price[$j+1]['price'].lang('ds_yuan')):sprintf(lang('o2o_errand_distance_content'),$o2o_errand_distance_price[$j+1]['interval_distance'],$o2o_errand_distance_price[$j+1]['price']);
//                if ($o2o_errand_distance_price[$j + 1]['start_distance'] < $o2o_errand_distance_price[$j]['end_distance']) {
//                    $this->error(sprintf(lang('o2o_errand_distance_content_error'),$o2o_errand_distance_price[$j]['title'],$o2o_errand_distance_price[$j+1]['title']));
//                }
            }
        }
        return serialize($o2o_errand_distance_price);
    }
    private function formatO2oErrandWeightPrice() {
        $o2o_errand_weight_price = input('param.o2o_errand_weight_price/a');
        if (!$o2o_errand_weight_price) {
            $this->error(lang('o2o_errand_weight_price_require'));
        }
        $o2o_errand_weight_price = array_values($o2o_errand_weight_price);
        $len = count($o2o_errand_weight_price);
        //冒泡排序法，按照从小到大排序
        for ($i = 1; $i < $len; $i++) {
            for ($j = 0; $j < $len - $i; $j++) {
                $o2o_errand_weight_price[$j]['start_weight']=($j>0 && isset($o2o_errand_weight_price[$j-1]['end_weight']))?$o2o_errand_weight_price[$j-1]['end_weight']:0;
                $o2o_errand_weight_price[$j]['end_weight']=abs(floatval($o2o_errand_weight_price[$j]['end_weight']));
                $o2o_errand_weight_price[$j]['interval_weight']=abs(floatval($o2o_errand_weight_price[$j]['interval_weight']));
                $o2o_errand_weight_price[$j]['price']=abs(floatval($o2o_errand_weight_price[$j]['price']));
                if(!$o2o_errand_weight_price[$j]['if_fixed'] && $o2o_errand_weight_price[$j]['interval_weight']==0){
                    $this->error('非固定费用时重量增量不能为0');
                }
                $o2o_errand_weight_price[$j+1]['start_weight']=$o2o_errand_weight_price[$j]['end_weight'];
                $o2o_errand_weight_price[$j+1]['end_weight']=abs(floatval($o2o_errand_weight_price[$j+1]['end_weight']));
                $o2o_errand_weight_price[$j+1]['interval_weight']=abs(floatval($o2o_errand_weight_price[$j+1]['interval_weight']));
                $o2o_errand_weight_price[$j+1]['price']=abs(floatval($o2o_errand_weight_price[$j+1]['price']));
                if(!$o2o_errand_weight_price[$j+1]['if_fixed'] && $o2o_errand_weight_price[$j+1]['interval_weight']==0){
                    $this->error('非固定费用时重量增量不能为0');
                }
                if ($o2o_errand_weight_price[$j]['start_weight'] > $o2o_errand_weight_price[$j + 1]['start_weight']) {
                    $temp = $o2o_errand_weight_price[$j + 1];
                    $o2o_errand_weight_price[$j + 1] = $o2o_errand_weight_price[$j];
                    $o2o_errand_weight_price[$j] = $temp;
                }
                $o2o_errand_weight_price[$j]['title']=$o2o_errand_weight_price[$j]['start_weight'].'~'.$o2o_errand_weight_price[$j]['end_weight'].lang('o2o_errand_kilogram');
                $o2o_errand_weight_price[$j]['content']=$o2o_errand_weight_price[$j]['if_fixed']?($o2o_errand_weight_price[$j]['price'].lang('ds_yuan')):sprintf(lang('o2o_errand_weight_content'),$o2o_errand_weight_price[$j]['interval_weight'],$o2o_errand_weight_price[$j]['price']);
                $o2o_errand_weight_price[$j+1]['title']=$o2o_errand_weight_price[$j+1]['start_weight'].'~'.$o2o_errand_weight_price[$j+1]['end_weight'].lang('o2o_errand_kilogram');
                $o2o_errand_weight_price[$j+1]['content']=$o2o_errand_weight_price[$j+1]['if_fixed']?($o2o_errand_weight_price[$j+1]['price'].lang('ds_yuan')):sprintf(lang('o2o_errand_weight_content'),$o2o_errand_weight_price[$j+1]['interval_weight'],$o2o_errand_weight_price[$j+1]['price']);
//                if ($o2o_errand_weight_price[$j + 1]['start_weight'] < $o2o_errand_weight_price[$j]['end_weight']) {
//                    $this->error(sprintf(lang('o2o_errand_weight_content_error'),$o2o_errand_weight_price[$j]['title'],$o2o_errand_weight_price[$j+1]['title']));
//                }
            }
        }
        return serialize($o2o_errand_weight_price);
    }
    private function formatO2oErrandTimePrice() {
        $o2o_errand_time_price = input('param.o2o_errand_time_price/a');
        if (!$o2o_errand_time_price) {
            $this->error(lang('o2o_errand_time_price_require'));
        }
        $o2o_errand_time_price = array_values($o2o_errand_time_price);
        $len = count($o2o_errand_time_price);
        //冒泡排序法，按照从小到大排序
        for ($i = 1; $i < $len; $i++) {
            for ($j = 0; $j < $len - $i; $j++) {
                if(isset($o2o_errand_time_price[$j]['time'])){
                    $temp= explode(',', $o2o_errand_time_price[$j]['time']);
                    unset($o2o_errand_time_price[$j]['time']);
                    $o2o_errand_time_price[$j]['start_time']=$temp[0];
                    $o2o_errand_time_price[$j]['end_time']=$temp[1];
                }
                
                if(isset($o2o_errand_time_price[$j+1]['time'])){
                    $temp= explode(',', $o2o_errand_time_price[$j+1]['time']);
                    unset($o2o_errand_time_price[$j+1]['time']);
                    $o2o_errand_time_price[$j+1]['start_time']=$temp[0];
                    $o2o_errand_time_price[$j+1]['end_time']=$temp[1];
                }
                
                $o2o_errand_time_price[$j]['price']=abs(floatval($o2o_errand_time_price[$j]['price']));
                
                $o2o_errand_time_price[$j+1]['price']=abs(floatval($o2o_errand_time_price[$j+1]['price']));
                
                if ($o2o_errand_time_price[$j]['start_time'] > $o2o_errand_time_price[$j + 1]['start_time']) {
                    $temp = $o2o_errand_time_price[$j + 1];
                    $o2o_errand_time_price[$j + 1] = $o2o_errand_time_price[$j];
                    $o2o_errand_time_price[$j] = $temp;
                }
                $temp=$o2o_errand_time_price[$j]['start_time'];
                if($temp>1440){
                    $this->error(lang('o2o_errand_time_start_error'));
                }
                $start_time=(($temp<600)?'0':'').($temp/60).':00';
                $temp=$o2o_errand_time_price[$j]['end_time'];
                $end_time=(($temp<600)?'0':(($temp>=1440)?'次日0':'')).(($temp/60)%24).':00';
                $o2o_errand_time_price[$j]['title']=$start_time.'~'.$end_time;
                $o2o_errand_time_price[$j]['content']=$o2o_errand_time_price[$j]['price'].lang('ds_yuan');
                $temp=$o2o_errand_time_price[$j+1]['start_time'];
                if($temp>1440){
                    $this->error(lang('o2o_errand_time_start_error'));
                }
                $start_time=(($temp<600)?'0':'').($temp/60).':00';
                $temp=$o2o_errand_time_price[$j+1]['end_time'];
                $end_time=(($temp<600)?'0':(($temp>=1440)?'次日0':'')).(($temp/60)%24).':00';
                $o2o_errand_time_price[$j+1]['title']=$start_time.'~'.$end_time;
                $o2o_errand_time_price[$j+1]['content']=$o2o_errand_time_price[$j+1]['price'].lang('ds_yuan');
                if ($o2o_errand_time_price[$j + 1]['start_time'] < $o2o_errand_time_price[$j]['end_time']) {
                    $this->error(sprintf(lang('o2o_errand_time_content_error'),$o2o_errand_time_price[$j]['title'],$o2o_errand_time_price[$j+1]['title']));
                }
            }
        }
        return serialize($o2o_errand_time_price);
    }
    

    protected function getAdminItemList() {
        $menu_array = array(
            array(
                'name' => 'distance_price',
                'text' => lang('o2o_errand_distance_price'),
                'url' => url('O2oErrand/distance_price')
            ),
            array(
                'name' => 'weight_price',
                'text' => lang('o2o_errand_weight_price'),
                'url' => url('O2oErrand/weight_price')
            ),
            array(
                'name' => 'time_price',
                'text' => lang('o2o_errand_time_price'),
                'url' => url('O2oErrand/time_price')
            ),
            array(
                'name' => 'other',
                'text' => lang('o2o_errand_other'),
                'url' => url('O2oErrand/other')
            ),
        );
        return $menu_array;
    }

}
